﻿@model GolfLeague.EditModels.RoundMatchesEditModel

<script type="text/javascript">
    $(document).ready(function () {
        $('input:button').button();
    });

    $(function () {
        $('#ScheduleDropDown').change(function () {
            var scheduleID = $(this).val();
            var options = '';

            if (scheduleID != '') {
                $.ajax({
                    url: '/Score/RoundDropdown',
                    type: 'POST',
                    dataType: 'json',
                    data: '{ scheduleID : "' + scheduleID + '"}',
                    contentType: 'application/json; charset=utf-8',
                    error: function (xhr, status, error) {
                        alert(xhr + '\n' + status + '\n' + error);
                    },
                    success: function (data) {
                        $.each(data.roundsDropDownList, function (i, value) {
                            options += '<option value="' + value.RoundID + '">' + value.RoundDate + '</option>';
                        });
                        $('#RoundDropDown').html(options);
                        var roundID = $('#RoundDropDown').val();
                        RenderMatches(roundID);
                    }
                });
            }
        });
    });

    $(function () {
        $('#RoundDropDown').change(function () {
            var roundID = $(this).val();
            RenderMatches(roundID);
        });
    });

    function RenderMatches(roundID) {
        if (roundID != '') {
            $.ajax({
                url: '/Scorecard/Matches?roundID=' + roundID,
                type: 'GET',
                dataType: 'html',
                //data: '{ roundID : "' + roundID + '"}',
                success: function (data) {
                    $('#Matches').html(data);
                }
            });
            $.ajax({
                url: '/Scorecard/Scorecards?roundID=' + roundID,
                type: 'GET',
                dataType: 'html',
                success: function (data) {
                    $('#Scorecards').html(data);
                }
            });
        }
    }

    $(function () {
        $('.GenerateScorecardButton').click(function (e) {
            e.preventDefault();
            var roundID = $('#RoundDropDown').val();
            //roundID = roundID.replace("generate-", "");

            if (roundID != '') {
                window.location = '/Scorecard/GenerateScorecards/' + roundID;
            }
        });
    });

    $(function () {
        $('.PrintScorecardButton').click(function (e) {
            e.preventDefault();
            var pp = window.open();

            pp.document.writeln('<!DOCTYPE html>');
            pp.document.writeln('<HTML><HEAD><title>Print Preview</title>');
            pp.document.writeln('<LINK href="/Content/Site.css" rel="stylesheet" type="text/css"/>');
            pp.document.writeln('</HEAD>');
            pp.document.writeln('<BODY bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0">');
            pp.document.writeln('<DIV CLASS="page"><DIV id="main">');
            pp.document.writeln('<INPUT ID="PRINT" type="button" value="Print" onclick="javascript:window.print();"><INPUT ID="CLOSE" type="button" value="Close" onclick="window.close();">');

            var roundID = $('#RoundDropDown').val();
            if (roundID != '') {
                $.ajax({
                    url: '/Scorecard/Scorecards?roundID=' + roundID,
                    type: 'GET',
                    dataType: 'html',
                    success: function (data) {
                        pp.document.writeln(data);
                        pp.document.writeln('</DIV></DIV>');
                        pp.document.writeln('</BODY></HTML>');
                    }
                });
            }
        });
    });

</script>
@{
    ViewBag.Title = "RoundScorecards";
}

<h2>RoundScorecards</h2>

@Html.DropDownList("ScheduleDropDown", new SelectList(Model.Schedules, "ScheduleID", "ScheduleName", Model.ScheduleID))
@Html.DropDownList("RoundDropDown", new SelectList(Model.RoundsDropDownList, "RoundID", "RoundDate", Model.RoundID))
<br /><br />
<div id="Matches">@Html.Partial("_Matches", Model.Matches)</div>
<div id="ScorecardButtons">
    <input type="button" class="GenerateScorecardButton" id="generate" value="Generate Scorecards" />
    <input type="button" class="PrintScorecardButton" id="print" value="Print Scorecards" />
</div>
<br />
<fieldset>
    <legend>Scorecards</legend>
    <div id="Scorecards">@Html.Partial("_Scorecards", Model.MatchScoreCards)</div>
</fieldset>